package avicit.cbb.techclass.billdefine.action;

import avicit.cbb.billdefine.service.api.BillDefineDynamicServiceApi;
import avicit.cbb.billdefine.service.api.BillDefineManagerIntfsServiceApi;
import avicit.cbb.tabledefine.service.CommTableManagerIntfsService;
import avicit.cbb.tabledefine.service.api.CommTableManagerIntfsServiceApi;
import avicit.cbb.techclass.billdefine.domain.CbbTabFormConfig;
import avicit.cbb.techclass.billdefine.service.api.BillDefineManagerServiceApi;
import avicit.cbb.techclass.tabledefine.commtablemanager.domain.CbbTabColumns;
import avicit.cbb.techclass.tabledefine.commtablemanager.action.service.CommTableManagerService;
import avicit.cbb.techclass.tabledefine.commtablemanager.action.service.api.CommTableManagerServiceApi;
import avicit.cbb.techclass.tabledefine.commtempletmanager.domain.CbbTempletCode;
import avicit.platform6.api.session.SessionHelper;
import avicit.platform6.api.sysshirolog.impl.AfterLoginSessionProcess;
import avicit.platform6.api.sysuser.SysUserAPI;
import avicit.platform6.api.sysuser.impl.SysUserAPImpl;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.RequestUtil;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.jdbc.Page;
import avicit.platform6.core.properties.PlatformConstant.OpResult;
import avicit.platform6.core.spring.SpringFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 金航数码科技有限责任公司
 * </p>
 * <p>
 * 作者：朱锋
 * </p>
 * <p>
 * 邮箱：zhufeng@avicit.com
 * </p>
 * <p>
 * 创建时间： 2014-9-26 上午9:28:49
 * </p>
 * <p>
 * 类说明：表单Controller
 * </p>
 * <p>
 * 修改记录：
 * </p>
 */
@Controller
@RequestMapping("/cbb/billDefineManagerController")
@SuppressWarnings("deprecation")
public class BillDefineManagerController {

	@Autowired
	private BillDefineManagerServiceApi billDefineManagerService;
	@Autowired
	private CommTableManagerServiceApi commTableManagerService;
	@Autowired
	CommSysInfoIntfsService commSysInfoIntfsService;
	@Autowired(required = true)
	private BillDefineManagerIntfsServiceApi billDefineManagerIntfsService;
	@Autowired(required = true)
	private BillDefineDynamicServiceApi billDefineDynamicService;
	@Autowired(required = true)
	private CommTableManagerIntfsServiceApi commTableManagerIntfsService;
	SysUserAPI sysUserLoaderI = SpringFactory.getBean(SysUserAPImpl.class);

	/**
	 * 获取表单列信息
	 *
	 * @author 朱锋
	 * @param response
	 * @param request
	 * @return ModelAndView
	 * @throws Exception
	 *             ModelAndView
	 */
	@RequestMapping(value = "/getCbbBillDefineData")
	public ModelAndView getCbbBillDefineData(HttpServletResponse response, HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView("");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String nodeName = request.getParameter("nodeName");
		String tableName = request.getParameter("tableName");
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("sysId", sysId);
		parameter.put("nodeId", nodeId);
		parameter.put("nodeName", nodeName);
		parameter.put("tableName", tableName);
		Paging<CbbTempletCode> page = new Paging<CbbTempletCode>();
		String pageNo = request.getParameter("page");// 取得当前页码
		String pageSize = request.getParameter("rows");// 取得一页显示的数量
		page.setPageSize(ComUtil.getPageSize(pageSize));
		page.setPageNo(ComUtil.getPageNo(pageNo));
		Page<Map<String, Object>> result = billDefineManagerService.getPageBillDetailList(page, parameter, SessionHelper.getCurrentOrgId(request));
		mav.addObject("total", result.getEntityCount()); // total键
		mav.addObject("rows", result.getEntities()); // rows键 存放每页记录 list
		return mav;
	}

	/**
	 * 表单列更新方法
	 *
	 * @param request
	 *            请求参数
	 * @return
	 */
	@RequestMapping("/updateCbbTabFormConfigData")
	@ResponseBody
	public Map<String,Object> updateCbbTabFormConfigData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String json = request.getParameter("param");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableId = request.getParameter("tableId");
		String nodeName = request.getParameter("nodeName");
		try {
			// 判断是否已经存在表单配置信息
			if (billDefineManagerService.checkHasForm2(tableId, nodeId)) {
				// 存在，那么更新表单配置信息表中的列配置属性
				JSONArray ja = JSONArray.fromObject(json);
				for (int i = 0; i < ja.size(); i++) {
					JSONObject jj = JSONObject.fromObject(ja.get(i));
					String formConfigId = jj.getString("ID");
					String formVertical = jj.getString("FORM_VERTICAL");
					String formHorizontal = jj.getString("FORM_HORIZONTAL");
					String colOrder = jj.getString("COL_ORDER");
					CbbTabFormConfig cbbTabFormConfig = billDefineManagerService.getcbbTabFormConfigById(formConfigId);
					BigDecimal formv = new BigDecimal(Integer.parseInt(formVertical));
					cbbTabFormConfig.setFormVertical(formv);
					BigDecimal formh = new BigDecimal(Integer.parseInt(formHorizontal));
					cbbTabFormConfig.setFormHorizontal(formh);
					BigDecimal formColOrder = new BigDecimal(Integer.parseInt(colOrder));//表单顺序
					cbbTabFormConfig.setAttribute11(formColOrder);
					billDefineManagerService.updateCbbTabForm(cbbTabFormConfig);
				}
				map.put("flag", OpResult.success);
			} else {
				// 不存在，那么新增表单配置信息表中的列配置属性
				billDefineManagerService.createCbbTabForm2(sysId, nodeName, ThreadContextHelper.getOrgId(), ThreadContextHelper.getDeptId(), tableId, nodeId, json);
				map.put("flag", OpResult.success);
			}

		} catch (Exception e) {
			map.put("flag", OpResult.failure);
			e.printStackTrace();
		}
		return map;
	}


	/**
	 * 表单列更新方法
	 *
	 * @param request
	 *            请求参数
	 * @return
	 */
	@RequestMapping("/updateCbbTabFormConfigArchiveData")
	@ResponseBody
	public Map<String,Object> updateCbbTabFormConfigArchiveData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String json = request.getParameter("param");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableId = request.getParameter("tableId");
		String nodeName =URLDecoder.decode(request.getParameter("nodeName"),"utf-8");
		String archiveStatus = request.getParameter("archiveStatus");
		Boolean checkHasForm = billDefineManagerService.checkHasForm(tableId, nodeId,archiveStatus);
		try {
			// 判断是否已经存在表单配置信息
			if (checkHasForm) {
				// 存在，那么更新表单配置信息表中的列配置属性
				JSONArray ja = JSONArray.fromObject(json);
				for (int i = 0; i < ja.size(); i++) {
					JSONObject jj = JSONObject.fromObject(ja.get(i));
					String formConfigId = jj.getString("ID");
					String formVertical = jj.getString("FORM_VERTICAL");
					String formHorizontal = jj.getString("FORM_HORIZONTAL");
					String colOrder = jj.getString("COL_ORDER");
					String colTabOrder = jj.getString("ATTRIBUTE_12");
					String colIsDetail = jj.getString("COL_IS_DETAIL");
					String colIsMust = jj.getString("COL_IS_MUST");
					String colIsVisible = jj.getString("COL_IS_VISIBLE");
					String colIsTabVisible = jj.getString("COL_IS_TAB_VISIBLE");
					String colIsReadOnly = jj.getString("ATTRIBUTE_04");//编辑是否只读
					CbbTabFormConfig cbbTabFormConfig = billDefineManagerService.getcbbTabFormConfigById(formConfigId);
					BigDecimal formv = null;
					try {
						formv = new BigDecimal(Integer.parseInt(formVertical));
					} catch (NumberFormatException e) {
						//SanXiaFiles update by huangijan 如果强转失败给默认值
						formv = new BigDecimal(i);
					}
					cbbTabFormConfig.setFormVertical(formv);
					BigDecimal formh = null;
					try {
						formh = new BigDecimal(Integer.parseInt(formHorizontal));
					} catch (NumberFormatException e) {
						formh = new BigDecimal(i);
					}
					cbbTabFormConfig.setFormHorizontal(formh);
					BigDecimal formColOrder = null;//表单顺序
					try {
						formColOrder = new BigDecimal(Integer.parseInt(colOrder));
					} catch (NumberFormatException e) {
						formColOrder = new BigDecimal(i);
					}
					cbbTabFormConfig.setAttribute11(formColOrder);
					BigDecimal formColTabOrder = null;//表格顺序
					try {
						formColTabOrder = new BigDecimal(Integer.parseInt(colTabOrder));
					} catch (NumberFormatException e) {
						formColTabOrder = new BigDecimal(i);
					}
					cbbTabFormConfig.setAttribute12(formColTabOrder);
					cbbTabFormConfig.setColIsDetail(colIsDetail);//是否详细显示
					cbbTabFormConfig.setAttribute01(colIsMust);//是否必著
					cbbTabFormConfig.setAttribute02(colIsTabVisible);//是否表格显示
					cbbTabFormConfig.setAttribute03(colIsVisible);//是否添加/编辑显示
					cbbTabFormConfig.setAttribute04(colIsReadOnly);//是否编辑/添加只读
					billDefineManagerService.updateCbbTabForm(cbbTabFormConfig);
				}
				map.put("flag", OpResult.success);
			} else {
				// 不存在，那么新增表单配置信息表中的列配置属性
				billDefineManagerService.createCbbTabForm(sysId, nodeName, ThreadContextHelper.getOrgId(), ThreadContextHelper.getDeptId(), tableId, nodeId, json,archiveStatus);
				map.put("flag", OpResult.success);
			}

		} catch (Exception e) {
			map.put("flag", OpResult.failure);
			e.printStackTrace();
		}
		//mav.setViewName("avicit/cbb/techclass/billdefine/billdefine_archive_common.jsp");
		return map;
	}
	/**
	 * 表单列重置方法
	 *
	 * @param request
	 *            请求参数
	 * @return
	 */
	@RequestMapping("/resetCbbTabFormConfigData")
	@ResponseBody
	public Map<String,Object> resetCbbTabFormConfigData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		//String json = request.getParameter("param");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableId = request.getParameter("tableId");
		//String nodeName = request.getParameter("nodeName");
		String archiveStatus = request.getParameter("archiveStatus");
		try {
			if(archiveStatus!=null && !"".equals(archiveStatus)){
				billDefineManagerService.resetFormDataByArchiveFormId(sysId,nodeId,tableId,archiveStatus);
			}else{
				billDefineManagerService.resetFormDataByFormId(sysId,nodeId,tableId);
			}
			map.put("flag", OpResult.success);
		} catch (Exception e) {
			map.put("flag", OpResult.failure);
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 跳转表单设计页面
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/toFormShow")
	public ModelAndView toFormShow(HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		String dialogId = request.getParameter("dialogId");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String nodeName = request.getParameter("nodeName");
		String tableName = request.getParameter("tableName");
		String archiveType = request.getParameter("archiveType");
		String tableId = commTableManagerService.getCbbTableId(sysId, tableName);
		String orgId = ThreadContextHelper.getOrgId();
		String deptId = ThreadContextHelper.getDeptId();

		String loginSecretLevel = SessionHelper.getLoginSysUser(request).getSecretLevel();//当前登录人密级
		Map<String,Object> lookUpMap = new HashMap<String, Object>();//获取通用代码参数集合
		lookUpMap.put("userSecretLevel", loginSecretLevel);
		Map<String, Map<String, String>> mapCode = this.billDefineManagerIntfsService.getBillLookUpCodeByIntfs(orgId, deptId, sysId, tableId, "", "3",lookUpMap);// 获取字段通用代码数据
		Map<String,Object> editMap = new HashMap<String, Object>();//获取编辑或详细页面字段参数集合
		editMap.put("nodeId", nodeId);
		Object sessionLanguageCode = request.getSession().getAttribute(AfterLoginSessionProcess.SESSION_CURRENT_LANGUAGE_CODE);
		editMap.put("languageCode", sessionLanguageCode);
		List<Map<String, Object>> editList = new ArrayList<>();
		String archiveStatus = "";
		if(archiveType != null && archiveType.equals("2") ){
			 archiveStatus = request.getParameter("archiveStatus");//1:未归档 2：著录表单 3：馆藏
			//SanXiaFiles update by huangjian start
			editMap.put("archiveFlag",archiveStatus);//3馆藏，2著录整理  1整理归档

			editList = this.billDefineManagerIntfsService.getEditOrDetailFieldsAndDataByIntfsItera(orgId, deptId, sysId, tableId, "", "3",editMap);
		}else{
			 editList = this.billDefineManagerIntfsService.getEditOrDetailFieldsAndDataByIntfs(orgId, deptId, sysId, tableId, "", "3",editMap);
		}
		Map<String, Object> colMap = new HashMap<String, Object>();
		colMap.put("tableId", tableId);
		colMap.put("nodeId", nodeId);
		colMap.put("sysId", sysId);
		colMap.put("flag", "3");
		colMap.put("orgId", orgId);
		colMap.put("deptId", deptId);
		//SanXiaFiles update by huangjian  start
		colMap.put("archiveStatus",archiveStatus);//1 预归档 2著录 3馆藏
		//Map<String, Object> dynamicMap = this.commTableManagerIntfsService.getColumnPageMap(colMap);
		Map<String, Object> dynamicMap = null;
		if ("2".equals(archiveType)) {
			//editFrom 列的信息
			dynamicMap = this.commTableManagerIntfsService.getColumnPageMapItera(colMap,editList);
		}else{
			dynamicMap = this.commTableManagerIntfsService.getColumnPageMap(colMap);
		}
		dynamicMap.put("MODEL_CODE", "mydocmanagerDetailFile");
		dynamicMap.put("FORM_CODE", "detailFileForm");
		String detailHtml = this.billDefineDynamicService.getDynamicDetailHtml(editList, mapCode, dynamicMap);
		mav.addObject("detailHtml", detailHtml);
		mav.addObject("dialogId", dialogId);
		mav.addObject("sysId", sysId);
		mav.addObject("nodeId", nodeId);
		mav.addObject("nodeName", nodeName);
		mav.addObject("tableName", tableName);
		mav.setViewName("avicit/cbb/techclass/billdefine/billdefine_show_manager");
		return mav;
	}

	/**
	 * 跳转表单设计页面
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/toRuleShow")
	public ModelAndView toRuleShow(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String dialogId = ServletRequestUtils.getStringParameter(request,"dialogId","");
		String sysId = ServletRequestUtils.getStringParameter(request,"sysId","");
		String selCodeOld = ServletRequestUtils.getStringParameter(request,"selCodeOld","");
		String module = ServletRequestUtils.getStringParameter(request,"module",""); // 模块标识（初始化字段维护模块为initField）
		mav.addObject("dialogId", dialogId);
		mav.addObject("sysId", sysId);
		mav.addObject("selCodeOld", selCodeOld);
		mav.addObject("module", module);
		mav.setViewName("avicit/cbb/techclass/tabledefine/commtablemanager/table_rule_manager");
		return mav;
	}

	/**
	 * 跳转自定义页面
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/toCustomSelect")
	public ModelAndView toCustomSelect(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String dialogId = request.getParameter("dialogId");
		String path = request.getParameter("path");
		String colName = request.getParameter("colName");
		mav.addObject("dialogId", dialogId);
		mav.addObject("path", path);
		mav.addObject("colName", colName);
		mav.setViewName(path);
		return mav;
	}

	/**
	 * 跳转表单设计应用场景页面
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/toFormApp")
	public ModelAndView toFormApp(HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String nodeName = request.getParameter("nodeName");
		String tableName = request.getParameter("tableName").toUpperCase();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("orgId", ThreadContextHelper.getOrgId());
		String tableId = commTableManagerService.getTableIdBysysIdAndTableName(sysId, tableName, param);
		List<CbbTabColumns> formShowListSearch = billDefineManagerService.getFormShowTableList(tableId, "", tableName, "4", sysId, ThreadContextHelper.getOrgId(),
				ThreadContextHelper.getDeptId());// 基本字段查询对象
		List<CbbTabColumns> formShowListTab = billDefineManagerService.getFormShowTableList(tableId, "", tableName, "2", sysId, SessionHelper.getCurrentOrgId(request),
				SessionHelper.getCurrentDeptId(request));// 基本字段列表对象
		Map<String, Map<String, String>> mapCode = billDefineManagerService.getRuleCodeList(formShowListSearch, "1", sysId, SessionHelper.getCurrentOrgId(request));// 查找字段生成方式为通用代码的集合
		String deptCols = billDefineManagerService.getUserDpetString(tableId, sysId, SessionHelper.getCurrentOrgId(request));// 查找选部门，选人的字段字符串
		// 获取字段数量
		int num = formShowListSearch.size() + formShowListTab.size();
		mav.addObject("sysId", sysId);
		mav.addObject("nodeId", nodeId);
		mav.addObject("nodeName", nodeName);
		mav.addObject("tableName", tableName);
		mav.addObject("formShowListSearch", formShowListSearch);
		mav.addObject("formShowListTab", formShowListTab);
		mav.addObject("num", num);
		mav.addObject("deptCols", deptCols);
		mav.addObject("mapCode", mapCode);
		mav.setViewName("avicit/cbb/techclass/billdefine/bill_define_manager_interface");
		return mav;
	}

	/**
	 * 获取列代码信息
	 *
	 * @author 朱锋
	 * @param response
	 * @param request
	 * @return ModelAndView
	 * @throws Exception
	 *             ModelAndView
	 */
	@RequestMapping(value = "/getformInitData")
	public ModelAndView getformInitData(HttpServletResponse response, HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView("");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableName = request.getParameter("tableName");
		tableName = URLDecoder.decode(tableName, "utf-8");
		String nodeName = request.getParameter("nodeName");
		nodeName = URLDecoder.decode(nodeName, "utf-8");
		String jsonData = request.getParameter("jsonData");
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("sysId", sysId);
		parameter.put("nodeId", nodeId);
		parameter.put("tableName", tableName);
		parameter.put("nodeName", nodeName);
		parameter.put("jsonData", jsonData);
		Paging<CbbTabColumns> page = new Paging<CbbTabColumns>();
		String pageNo = request.getParameter("page");// 取得当前页码
		String pageSize = request.getParameter("rows");// 取得一页显示的数量
		page.setPageSize(ComUtil.getPageSize(pageSize));
		page.setPageNo(ComUtil.getPageNo(pageNo));
		Page<Map<String, Object>> result = billDefineManagerService.getPageCodeDetailList(page, parameter, SessionHelper.getCurrentOrgId(request), SessionHelper.getCurrentDeptId(request));
		mav.addObject("total", result.getEntityCount()); // total键
		mav.addObject("rows", result.getEntities()); // rows键 存放每页记录 list
		return mav;
	}

	/**
	 * 表单数据跳转添加取值
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toBilldefineAdd")
	public ModelAndView toBilldefineAdd(HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		String dialogId = request.getParameter("dialogId");
		String toJspUrl = request.getParameter("toJspUrl");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableName = request.getParameter("tableName");
		tableName = URLDecoder.decode(tableName, "utf-8");
		String nodeName = request.getParameter("nodeName");
		String id = request.getParameter("id");// 选中数据的ID
		String tableId = commTableManagerService.getTableIdBysnId(sysId, nodeId);
		List<CbbTabColumns> formShowList = billDefineManagerService.getFormShowTableListToAdd(tableId, "", tableName, "1", sysId, SessionHelper.getCurrentOrgId(request),
				SessionHelper.getCurrentDeptId(request), id);// 基本字段对象
		Map<String, Map<String, String>> mapCode = billDefineManagerService.getRuleCodeList(formShowList, "1", sysId, SessionHelper.getCurrentOrgId(request));// 查找字段生成方式为通用代码的集合
		String deptCols = billDefineManagerService.getUserDpetString(tableId, sysId, SessionHelper.getCurrentOrgId(request));// 查找选部门，选人的字段字符串
		mav.addObject("dialogId", dialogId);
		mav.addObject("sysId", sysId);
		mav.addObject("nodeId", nodeId);
		mav.addObject("nodeName", nodeName);
		mav.addObject("tableName", tableName);
		mav.addObject("formShowList", formShowList);
		mav.addObject("deptCols", deptCols);
		mav.addObject("mapCode", mapCode);
		mav.setViewName(toJspUrl);
		return mav;
	}

	/**
	 * 表单数据跳转编辑取值
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toBilldefineEdit")
	public ModelAndView toBilldefineEdit(HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		String dialogId = request.getParameter("dialogId");
		String toJspUrl = request.getParameter("toJspUrl");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableName = request.getParameter("tableName");
		tableName = URLDecoder.decode(tableName, "utf-8");
		String nodeName = request.getParameter("nodeName");
		String id = request.getParameter("id");
		String tableId = commTableManagerService.getTableIdBysnId(sysId, nodeId);
		List<CbbTabColumns> formShowList = billDefineManagerService.getFormShowTableList(tableId, id, tableName, "1", sysId, SessionHelper.getCurrentOrgId(request),
				SessionHelper.getCurrentDeptId(request));// 基本字段对象
		Map<String, Map<String, String>> mapCode = billDefineManagerService.getRuleCodeList(formShowList, "1", sysId, SessionHelper.getCurrentOrgId(request));// 查找字段生成方式为通用代码的集合
		String deptCols = billDefineManagerService.getUserDpetString(tableId, sysId, SessionHelper.getCurrentOrgId(request));// 查找选部门，选人的字段字符串
		mav.addObject("dialogId", dialogId);
		mav.addObject("sysId", sysId);
		mav.addObject("nodeId", nodeId);
		mav.addObject("nodeName", nodeName);
		mav.addObject("tableName", tableName);
		mav.addObject("formShowList", formShowList);
		mav.addObject("mapCode", mapCode);
		mav.addObject("id", id);
		mav.addObject("deptCols", deptCols);
		mav.setViewName(toJspUrl);
		return mav;
	}

	/**
	 * 表单数据新增方法
	 *
	 * @param request
	 *            请求参数
	 * @return
	 */
	@RequestMapping("/insertFormData")
	public ModelAndView insertFormData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView("");
		String json = request.getParameter("param");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableName = request.getParameter("tableName");
		String nodeName = request.getParameter("nodeName");
		nodeName = URLDecoder.decode(nodeName, "utf-8");
		try {
			billDefineManagerService.insertFormData(json, sysId, tableName, nodeName, nodeId, SessionHelper.getCurrentOrgId(request), SessionHelper.getCurrentDeptId(request),
					SessionHelper.getLoginSysUserId(request), RequestUtil.getClientIp(request));
			mav.addObject("flag", OpResult.success);
		} catch (Exception e) {
			mav.addObject("flag", OpResult.failure);
		}
		mav.setView(null);
		return mav;
	}

	/**
	 * 表单数据更新方法
	 *
	 * @param request
	 *            请求参数
	 * @return
	 */
	@RequestMapping("/updateFormData")
	public ModelAndView updateFormData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView("");
		String json = request.getParameter("param");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableName = request.getParameter("tableName");
		tableName = URLDecoder.decode(tableName, "utf-8");
		String nodeName = request.getParameter("nodeName");
		String id = request.getParameter("id");
		try {
			billDefineManagerService.updateFormData(json, sysId, tableName, nodeName, nodeId, id, SessionHelper.getCurrentOrgId(request), SessionHelper.getCurrentDeptId(request));
			mav.addObject("flag", OpResult.success);
		} catch (Exception e) {
			mav.addObject("flag", OpResult.failure);
		}
		mav.setView(null);
		return mav;
	}

	/**
	 * 表单数据删除方法
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/deleteFormData")
	public ModelAndView deleteFormData(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		try {
			String sysId = request.getParameter("sysId");
			sysId = "LANTOP";
			// String nodeId = request.getParameter("nodeId");
			String tableName = request.getParameter("tableName");
			tableName = URLDecoder.decode(tableName, "utf-8");
			// String nodeName = request.getParameter("nodeName");
			String dataid = request.getParameter("id");
			if (dataid != null && !"".equals(dataid)) {
				billDefineManagerService.deleteFormDataById(dataid, tableName, sysId, SessionHelper.getCurrentOrgId(request), SessionHelper.getCurrentDeptId(request));
				mav.addObject("flag", OpResult.success);
			}
		} catch (Exception e) {
			mav.addObject("flag", OpResult.failure);
		}
		mav.setView(null);
		return mav;
	}

	/**
	 * 获取表单管理列表信息
	 *
	 * @author 朱锋
	 * @param response
	 * @param request
	 * @return ModelAndView
	 * @throws Exception
	 *             ModelAndView
	 */
	@RequestMapping(value = "/getCbbBillDefineManagerData")
	@ResponseBody
	public Map<String,Object> getCbbBillDefineManagerData(HttpServletResponse response, HttpServletRequest request) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";
		String nodeId = request.getParameter("nodeId");
		String tableName = request.getParameter("tableName");
		String tableId = request.getParameter("tableId");
		String archiveStatus = request.getParameter("archiveStatus");
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("sysId", sysId);
		parameter.put("nodeId", nodeId);
		parameter.put("tableId", tableId);
		parameter.put("tableName", tableName);
		parameter.put("archiveStatus", archiveStatus);
		Paging<CbbTempletCode> page = new Paging<CbbTempletCode>();
		String pageNo = request.getParameter("page");// 取得当前页码
		String pageSize = request.getParameter("rows");// 取得一页显示的数量
		page.setPageSize(ComUtil.getPageSize(pageSize));
		page.setPageNo(ComUtil.getPageNo(pageNo));
		Page<Map<String, Object>> result = new Page<Map<String, Object>>();
		// 判断该节点下的表是否已经创建表单
		// SanXiaFiles Create By Tangkun Start
		String inputWord1 = request.getParameter("inputWord");
		String inputWord ="";
		if(inputWord1 != null && !"".equals(inputWord1)){
			inputWord=URLDecoder.decode(inputWord1,"utf-8");
			parameter.put("inputWord", inputWord);
			// parameter.put("filter_LIKE_phase", inputWord);
		}
			// SanXiaFiles Create By Tangkun End
		boolean checkHasForm ;
		if(archiveStatus != null && !"".equals(archiveStatus)){
			checkHasForm = billDefineManagerService.checkHasForm(tableId, nodeId,archiveStatus);
		}else{
			checkHasForm = billDefineManagerService.checkHasForm2(tableId, nodeId);
				}
		if (checkHasForm) {
			// 存在，那么加载表单配置信息表中的列配置属性
			result = billDefineManagerService.getPageBillFormConfigList(page, parameter);
		} else {
			// 不存在，那么加载模版库表的表单信息
			result = billDefineManagerService.getPageBillDbList(page, parameter, ThreadContextHelper.getOrgId());
		}
		map.put("total", result.getEntityCount()); // total键
		map.put("rows", result.getEntities()); // rows键 存放每页记录 list
		return map;
	}

	/***
	 * 判断表单管理树节点对应表是否有数据
	 * @author 郭亮
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception ModelAndView
	 * @throws
	 */
	@RequestMapping("/checkNodeHasData")
	@ResponseBody
	public Map<String,Object> checkNodeHasData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String tableId = request.getParameter("tableId");
		int dataCount = billDefineManagerService.getTableDataCount(tableId);
		map.put("dataCount", dataCount);
		return map;
	}

	/**
	 * 设置模版列数
	 * @author 郭亮
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception ModelAndView
	 */
	@RequestMapping("/setModelColumn")
	@ResponseBody
	public Map<String,Object> setModelColumn(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> map = new HashMap<>();
		String nodeId = request.getParameter("nodeId");
		String tableId = request.getParameter("tableId");
		String columnNum = request.getParameter("columnNum");
		String archiveStatus = request.getParameter("archiveStatus");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("nodeId", nodeId);
		param.put("tableId", tableId);
		param.put("columnNum", columnNum);
		if(!StringUtils.isEmpty(archiveStatus)){
			param.put("columnNum", columnNum);
		}
		try {
			billDefineManagerService.setModelColumn(param);
			map.put("flag", "success");
		} catch (Exception e) {
			map.put("flag", "failure");
		}
		return map;
	}

	/**
	 * 获取模板列数
	 * @author 郭亮
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/getModelColumn")
	@ResponseBody
	public Map<String,Object> getModelColumn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String nodeId = request.getParameter("nodeId");
		String tableId = request.getParameter("tableId");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("nodeId", nodeId);
		param.put("tableId", tableId);
		String columnNum = billDefineManagerService.getModelColumn(param);
		map.put("columnNum", columnNum);
		return map;
	}
}
